/*     */ package com.zimbra.soap;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.ElementFactory;
/*     */ import com.zimbra.common.soap.HeaderConstants;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ShareInfoData;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.User;
/*     */ import com.zimbra.cs.mailbox.acl.AclPushSerializer;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.servlet.continuation.ResumeContinuationListener;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.cs.session.SoapSession;
/*     */ import com.zimbra.cs.session.SoapSession.PushChannel;
/*     */ import com.zimbra.cs.session.SoapSession.RegisterNotificationResult;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.dom4j.QName;
/*     */ import org.eclipse.jetty.continuation.Continuation;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ZimbraSoapContext
/*     */ {
/*     */   final class SessionInfo
/*     */   {
/*     */     String sessionId;
/*     */     int sequence;
/*     */     boolean created;
/*     */     
/*     */     SessionInfo(String id, int seqNo, boolean newSession)
/*     */     {
/*  72 */       this.sessionId = id;this.sequence = seqNo;this.created = newSession;
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/*  77 */       return this.sessionId;
/*     */     }
/*     */     
/*     */     private class SoapPushChannel implements SoapSession.PushChannel {
/*     */       private final boolean mLocalChangesOnly;
/*     */       
/*  83 */       SoapPushChannel(boolean localOnly) { this.mLocalChangesOnly = localOnly; }
/*     */       
/*     */ 
/*     */       public void closePushChannel()
/*     */       {
/*  88 */         ZimbraSoapContext.this.signalNotification(true);
/*     */       }
/*     */       
/*     */       public int getLastKnownSequence()
/*     */       {
/*  93 */         return ZimbraSoapContext.SessionInfo.this.sequence;
/*     */       }
/*     */       
/*     */       public ZimbraSoapContext getSoapContext()
/*     */       {
/*  98 */         return ZimbraSoapContext.this;
/*     */       }
/*     */       
/*     */       public boolean localChangesOnly()
/*     */       {
/* 103 */         return this.mLocalChangesOnly;
/*     */       }
/*     */       
/*     */       public boolean isPersistent()
/*     */       {
/* 108 */         return false;
/*     */       }
/*     */       
/*     */       public void notificationsReady()
/*     */       {
/* 113 */         ZimbraSoapContext.this.signalNotification(false);
/*     */       }
/*     */     }
/*     */     
/*     */     SoapSession.PushChannel getPushChannel(boolean localChangesOnly) {
/* 118 */       return new SoapPushChannel(localChangesOnly);
/*     */     }
/*     */   }
/*     */   
/* 122 */   private static final Log sLog = LogFactory.getLog(ZimbraSoapContext.class);
/*     */   
/*     */   public static final int MAX_HOP_COUNT = 5;
/*     */   
/*     */   private ZAuthToken mRawAuthToken;
/*     */   
/*     */   private AuthToken mAuthToken;
/*     */   private String mAuthTokenAccountId;
/*     */   private String mRequestedAccountId;
/*     */   private SoapProtocol mRequestProtocol;
/*     */   private SoapProtocol mResponseProtocol;
/* 133 */   private boolean mChangeConstraintType = true;
/* 134 */   private int mMaximumChangeId = -1;
/*     */   
/*     */   private boolean mSessionEnabled;
/*     */   private boolean mSessionProxied;
/*     */   private SessionInfo mSessionInfo;
/*     */   private boolean mUnqualifiedItemIds;
/*     */   private boolean mWaitForNotifications;
/* 141 */   private boolean mCanceledWaitForNotifications = false;
/*     */   
/*     */   private ResumeContinuationListener continuationResume;
/*     */   
/*     */   private ProxyTarget mProxyTarget;
/*     */   private boolean mIsProxyRequest;
/*     */   private int mHopCount;
/*     */   private boolean mMountpointTraversed;
/*     */   private String mUserAgent;
/*     */   private String mRequestIP;
/*     */   private String mVia;
/*     */   private String soapRequestId;
/*     */   
/*     */   public ZimbraSoapContext(AuthToken authToken, String accountId, SoapProtocol reqProtocol, SoapProtocol respProtocol)
/*     */     throws ServiceException
/*     */   {
/* 157 */     this(authToken, accountId, reqProtocol, respProtocol, 0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(AuthToken authToken, String accountId, SoapProtocol reqProtocol, SoapProtocol respProtocol, int hopCount)
/*     */     throws ServiceException
/*     */   {
/* 169 */     this.mAuthToken = authToken;
/* 170 */     this.mRawAuthToken = authToken.toZAuthToken();
/* 171 */     this.mAuthTokenAccountId = authToken.getAccountId();
/* 172 */     this.mRequestedAccountId = accountId;
/* 173 */     this.mRequestProtocol = reqProtocol;
/* 174 */     this.mResponseProtocol = respProtocol;
/* 175 */     this.mSessionEnabled = false;
/* 176 */     this.mHopCount = hopCount;
/*     */     
/* 178 */     this.mUserAgent = (this.mRequestIP = this.mVia = null);
/* 179 */     this.soapRequestId = null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(ZimbraSoapContext zsc)
/*     */     throws ServiceException
/*     */   {
/* 189 */     this(zsc, zsc.mRequestedAccountId);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(ZimbraSoapContext zsc, String targetAccountId)
/*     */     throws ServiceException
/*     */   {
/* 201 */     this(zsc, targetAccountId, null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(ZimbraSoapContext zsc, String targetAccountId, Session session)
/*     */     throws ServiceException
/*     */   {
/* 215 */     this(zsc, null, targetAccountId, session);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(ZimbraSoapContext zsc, AuthToken authToken, String targetAccountId, Session session)
/*     */     throws ServiceException
/*     */   {
/* 225 */     this.mUserAgent = zsc.mUserAgent;
/* 226 */     this.mRequestIP = zsc.mRequestIP;
/* 227 */     this.mVia = zsc.mVia;
/* 228 */     this.soapRequestId = zsc.soapRequestId;
/*     */     
/* 230 */     this.mRawAuthToken = (authToken == null ? zsc.mRawAuthToken : authToken.toZAuthToken());
/* 231 */     this.mAuthToken = (authToken == null ? zsc.mAuthToken : authToken);
/* 232 */     this.mAuthTokenAccountId = (authToken == null ? zsc.mAuthTokenAccountId : authToken.getAccountId());
/*     */     
/* 234 */     this.mRequestedAccountId = targetAccountId;
/* 235 */     this.mRequestProtocol = zsc.mRequestProtocol;
/* 236 */     this.mResponseProtocol = zsc.mResponseProtocol;
/* 237 */     this.mSessionInfo = zsc.mSessionInfo;
/* 238 */     this.mSessionEnabled = zsc.mSessionEnabled;
/* 239 */     this.mSessionProxied = true;
/* 240 */     this.mUnqualifiedItemIds = zsc.mUnqualifiedItemIds;
/* 241 */     this.mMountpointTraversed = zsc.mMountpointTraversed;
/* 242 */     setHopCount(zsc.mHopCount + 1);
/* 243 */     if (session != null) {
/* 244 */       this.mSessionEnabled = true;
/* 245 */       this.mSessionInfo = new SessionInfo(session.getSessionId(), (session instanceof SoapSession) ? ((SoapSession)session).getCurrentNotificationSequence() : 0, false);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraSoapContext(Element ctxt, QName requestName, DocumentHandler handler, Map<String, Object> context, SoapProtocol requestProtocol)
/*     */     throws ServiceException
/*     */   {
/* 260 */     if ((ctxt != null) && (!ctxt.getQName().equals(HeaderConstants.CONTEXT))) {
/* 261 */       throw new IllegalArgumentException("expected ctxt, got: " + ctxt.getQualifiedName());
/*     */     }
/* 263 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 266 */     this.mResponseProtocol = (this.mRequestProtocol = requestProtocol);
/* 267 */     Element eFormat = ctxt == null ? null : ctxt.getOptionalElement("format");
/* 268 */     if (eFormat != null) {
/* 269 */       String format = eFormat.getAttribute("type", "xml");
/* 270 */       if ((format.equals("xml")) && (requestProtocol == SoapProtocol.SoapJS)) {
/* 271 */         this.mResponseProtocol = SoapProtocol.Soap12;
/* 272 */       } else if (format.equals("js")) {
/* 273 */         this.mResponseProtocol = SoapProtocol.SoapJS;
/*     */       }
/*     */     }
/*     */     try {
/* 277 */       this.mAuthToken = AuthProvider.getAuthToken(ctxt, context);
/* 278 */       if (this.mAuthToken != null) {
/* 279 */         if ((this.mAuthToken.isExpired()) || (!this.mAuthToken.isRegistered())) {
/* 280 */           boolean voidOnExpired = false;
/*     */           
/* 282 */           if (ctxt != null) {
/* 283 */             Element eAuthTokenControl = ctxt.getOptionalElement("authTokenControl");
/* 284 */             if (eAuthTokenControl != null) {
/* 285 */               voidOnExpired = eAuthTokenControl.getAttributeBool("voidOnExpired", false);
/*     */             }
/*     */           }
/*     */           
/* 289 */           if (voidOnExpired)
/*     */           {
/* 291 */             this.mAuthToken = null;
/*     */           } else {
/* 293 */             throw ServiceException.AUTH_EXPIRED();
/*     */           }
/*     */         } else {
/* 296 */           this.mRawAuthToken = this.mAuthToken.toZAuthToken();
/* 297 */           this.mAuthTokenAccountId = this.mAuthToken.getAccountId();
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (AuthTokenException e) {
/* 302 */       this.mAuthToken = null;
/* 303 */       if (sLog.isDebugEnabled()) {
/* 304 */         sLog.debug("ZimbraContext AuthToken error: " + e.getMessage(), e);
/*     */       }
/*     */     }
/*     */     
/* 308 */     Element eAccount = ctxt == null ? null : ctxt.getOptionalElement("account");
/* 309 */     if (eAccount != null) {
/* 310 */       String key = eAccount.getAttribute("by", null);
/* 311 */       String value = eAccount.getText();
/*     */       
/* 313 */       if (key == null) {
/* 314 */         this.mRequestedAccountId = null;
/* 315 */       } else if (key.equals("name")) {
/* 316 */         if (this.mAuthToken == null) {
/* 317 */           throw ServiceException.AUTH_REQUIRED();
/*     */         }
/* 319 */         Account account = prov.get(Key.AccountBy.name, value, this.mAuthToken);
/* 320 */         if (account == null) {
/* 321 */           if (!this.mAuthToken.isAdmin()) {
/* 322 */             throw ServiceException.DEFEND_ACCOUNT_HARVEST(value);
/*     */           }
/* 324 */           throw AccountServiceException.NO_SUCH_ACCOUNT(value);
/*     */         }
/*     */         
/*     */ 
/* 328 */         this.mRequestedAccountId = account.getId();
/* 329 */         validateDelegatedAccess(account, handler, requestName, value);
/* 330 */       } else if (key.equals("id")) {
/* 331 */         if (this.mAuthToken == null) {
/* 332 */           throw ServiceException.AUTH_REQUIRED();
/*     */         }
/* 334 */         Account account = prov.get(Key.AccountBy.id, value, this.mAuthToken);
/* 335 */         if (account == null) {
/* 336 */           if (!this.mAuthToken.isAdmin()) {
/* 337 */             throw ServiceException.DEFEND_ACCOUNT_HARVEST(value);
/*     */           }
/* 339 */           throw AccountServiceException.NO_SUCH_ACCOUNT(value);
/*     */         }
/*     */         
/*     */ 
/* 343 */         this.mRequestedAccountId = value;
/* 344 */         validateDelegatedAccess(account, handler, requestName, value);
/*     */       } else {
/* 346 */         throw ServiceException.INVALID_REQUEST("unknown value for by: " + key, null);
/*     */       }
/*     */       
/* 349 */       this.mMountpointTraversed = eAccount.getAttributeBool("link", false);
/*     */     } else {
/* 351 */       this.mRequestedAccountId = null;
/*     */     }
/*     */     
/*     */ 
/* 355 */     if (ctxt != null) {
/* 356 */       int hopCount = (int)Math.max(ctxt.getAttributeLong("hops", 0L), 0L);
/* 357 */       setHopCount(hopCount);
/*     */     }
/*     */     
/*     */ 
/* 361 */     Element change = ctxt == null ? null : ctxt.getOptionalElement("change");
/* 362 */     if (change != null) {
/*     */       try {
/* 364 */         String token = change.getAttribute("token", "-1");
/* 365 */         int delimiter = token.indexOf('-');
/*     */         
/* 367 */         this.mMaximumChangeId = Integer.parseInt(delimiter < 1 ? token : token.substring(0, delimiter));
/* 368 */         if (change.getAttribute("type", "mod").equals("mod")) {
/* 369 */           this.mChangeConstraintType = true;
/*     */         } else {
/* 371 */           this.mChangeConstraintType = false;
/*     */         }
/*     */       }
/*     */       catch (NumberFormatException nfe) {}
/*     */     }
/* 376 */     this.mIsProxyRequest = false;
/* 377 */     String targetServerId = ctxt == null ? null : ctxt.getAttribute("targetServer", null);
/* 378 */     if (targetServerId != null) {
/* 379 */       HttpServletRequest req = (HttpServletRequest)context.get("servlet.request");
/* 380 */       if (req != null) {
/* 381 */         this.mProxyTarget = new ProxyTarget(targetServerId, this.mAuthToken, req);
/* 382 */         this.mIsProxyRequest = (!this.mProxyTarget.isTargetLocal());
/*     */       } else {
/* 384 */         sLog.warn("Missing SERVLET_REQUEST key in request context");
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 390 */     int seqNo = -1;
/* 391 */     Element notify = ctxt == null ? null : ctxt.getOptionalElement("notify");
/* 392 */     if (notify != null) {
/* 393 */       seqNo = (int)notify.getAttributeLong("seq", 0L);
/*     */     }
/*     */     
/*     */ 
/* 397 */     if (ctxt != null) {
/* 398 */       boolean suppress = ctxt.getOptionalElement("nonotify") != null;
/* 399 */       suppress |= ctxt.getOptionalElement("nosession") != null;
/*     */       
/* 401 */       if (!suppress) {
/* 402 */         Element session = ctxt.getOptionalElement("session");
/* 403 */         if (session != null) {
/* 404 */           this.mSessionEnabled = true;
/* 405 */           this.mSessionProxied = session.getAttributeBool("proxy", false);
/*     */           
/* 407 */           String sessionId = null;
/* 408 */           if ("".equals(sessionId = session.getTextTrim()))
/* 409 */             sessionId = session.getAttribute("id", null);
/* 410 */           if (sessionId != null) {
/* 411 */             this.mSessionInfo = new SessionInfo(sessionId, (int)session.getAttributeLong("seq", seqNo), false);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 417 */     this.mUnqualifiedItemIds = ((ctxt != null) && (ctxt.getOptionalElement("noqualify") != null));
/*     */     
/*     */ 
/*     */ 
/* 421 */     Element userAgent = ctxt == null ? null : ctxt.getOptionalElement("userAgent");
/* 422 */     if (userAgent != null) {
/* 423 */       String name = userAgent.getAttribute("name", null);
/* 424 */       String version = userAgent.getAttribute("version", null);
/* 425 */       if (!Strings.isNullOrEmpty(name)) {
/* 426 */         if (!Strings.isNullOrEmpty(version)) {
/* 427 */           this.mUserAgent = (name + "/" + version);
/*     */         } else {
/* 429 */           this.mUserAgent = name;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 434 */     if (ctxt != null) {
/* 435 */       Element via = ctxt.getOptionalElement("via");
/* 436 */       if (via != null) {
/* 437 */         this.mVia = via.getText();
/*     */       }
/* 439 */       if (this.soapRequestId == null) {
/* 440 */         Element reqId = ctxt.getOptionalElement("soapId");
/* 441 */         if (null != reqId) {
/* 442 */           this.soapRequestId = reqId.getText();
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 447 */     this.mRequestIP = ((String)context.get("request.ip"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void validateDelegatedAccess(Account targetAccount, DocumentHandler handler, QName requestName, String requestedKey)
/*     */     throws ServiceException
/*     */   {
/* 464 */     if (!isDelegatedRequest()) {
/* 465 */       return;
/*     */     }
/*     */     
/* 468 */     if ((handler != null) && (handler.handlesAccountHarvesting())) {
/* 469 */       return;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 478 */     Account authAccount = null;
/* 479 */     boolean isAdmin = AuthToken.isAnyAdmin(this.mAuthToken);
/*     */     
/* 481 */     if (!"99999999-9999-9999-9999-999999999999".equals(this.mAuthToken.getAccountId())) {
/* 482 */       authAccount = this.mAuthToken.getAccount();
/* 483 */       if ((isAdmin) && (AccessManager.getInstance().canAccessAccount(this.mAuthToken, targetAccount, true)))
/*     */       {
/* 485 */         return;
/*     */       }
/*     */       
/* 488 */       if ((isAdmin) && (handler != null) && (handler.defendsAgainstDelegateAdminAccountHarvesting())) {
/* 489 */         return;
/*     */       }
/*     */       
/* 492 */       if (AccessManager.getInstance().canAccessAccount(this.mAuthToken, targetAccount, false))
/*     */       {
/* 494 */         return;
/*     */       }
/*     */     }
/*     */     
/* 498 */     String externalEmail = null;
/* 499 */     if ((authAccount != null) && (authAccount.getBooleanAttr("zimbraIsExternalVirtualAccount", false))) {
/* 500 */       externalEmail = authAccount.getAttr("zimbraExternalUserMailAddress", externalEmail);
/*     */     }
/*     */     
/* 503 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 506 */     boolean needRecheck = false;
/*     */     do {
/* 508 */       String[] sharedItems = targetAccount.getSharedItem();
/* 509 */       Set<String> groupIds = null;
/* 510 */       for (String sharedItem : sharedItems) {
/* 511 */         ShareInfoData shareData = AclPushSerializer.deserialize(sharedItem);
/* 512 */         switch (shareData.getGranteeTypeCode()) {
/*     */         case 1: 
/* 514 */           if ((authAccount != null) && (authAccount.getId().equals(shareData.getGranteeId()))) {
/*     */             return;
/*     */           }
/*     */           break;
/*     */         case 7: 
/* 519 */           if (shareData.getGranteeId().equals(externalEmail)) {
/*     */             return;
/*     */           }
/*     */           break;
/*     */         case 6: 
/* 524 */           return;
/*     */         case 2: 
/* 526 */           if (authAccount != null) {
/* 527 */             if (groupIds == null) {
/* 528 */               groupIds = new HashSet();
/*     */             }
/* 530 */             groupIds.add(shareData.getGranteeId());
/*     */           }
/*     */           break;
/*     */         case 3: 
/* 534 */           if (authAccount != null) {
/*     */             return;
/*     */           }
/*     */           break;
/*     */         case 4: 
/* 539 */           if ((authAccount != null) && (authAccount.getDomainId() != null) && (authAccount.getDomainId().equals(shareData.getGranteeId()))) {
/*     */             return;
/*     */           }
/*     */           
/*     */           break;
/*     */         case 5: 
/* 545 */           if ((authAccount != null) && (authAccount.getCOSId() != null) && (authAccount.getCOSId().equals(shareData.getGranteeId()))) {
/*     */             return;
/*     */           }
/*     */           
/*     */           break;
/*     */         case 8: 
/* 551 */           if (((authAccount instanceof GuestAccount)) && (this.mAuthToken.getAccessKey() != null)) {
/*     */             return;
/*     */           }
/*     */           break;
/*     */         }
/*     */         
/*     */       }
/* 558 */       if (groupIds != null) {
/* 559 */         for (String groupId : groupIds) {
/* 560 */           if (prov.inACLGroup(authAccount, groupId)) {
/* 561 */             return;
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 566 */       if (needRecheck)
/*     */         break;
/* 568 */       if (!Provisioning.onLocalServer(targetAccount))
/*     */       {
/*     */ 
/* 571 */         prov.reload(targetAccount);
/* 572 */         needRecheck = true;
/*     */       }
/* 574 */     } while (needRecheck);
/*     */     
/*     */ 
/* 577 */     AccessManager accessMgr = AccessManager.getInstance();
/* 578 */     if ((accessMgr.canDo(authAccount, targetAccount, Rights.User.R_sendAs, isAdmin)) || (accessMgr.canDo(authAccount, targetAccount, Rights.User.R_sendOnBehalfOf, isAdmin)))
/*     */     {
/* 580 */       return;
/*     */     }
/*     */     
/* 583 */     throw ServiceException.DEFEND_ACCOUNT_HARVEST(requestedKey);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void recordMountpointTraversal()
/*     */     throws ServiceException
/*     */   {
/* 598 */     if (this.mMountpointTraversed)
/* 599 */       throw ServiceException.PERM_DENIED("cannot mount a mountpoint");
/* 600 */     this.mMountpointTraversed = true;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 605 */     String sessionPart = this.mSessionEnabled ? ", session=" + this.mSessionInfo : "";
/* 606 */     return "LC(mbox=" + this.mAuthTokenAccountId + sessionPart + ")";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getRequestedAccountId()
/*     */   {
/* 613 */     return this.mRequestedAccountId != null ? this.mRequestedAccountId : this.mAuthTokenAccountId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getAuthtokenAccountId()
/*     */   {
/* 620 */     return this.mAuthTokenAccountId;
/*     */   }
/*     */   
/*     */ 
/*     */   void setAuthToken(AuthToken auth)
/*     */   {
/* 626 */     this.mAuthToken = auth;
/* 627 */     this.mAuthTokenAccountId = auth.getAccountId();
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean isDelegatedRequest()
/*     */   {
/* 633 */     return (this.mAuthTokenAccountId != null) && (!this.mAuthTokenAccountId.equalsIgnoreCase(getRequestedAccountId()));
/*     */   }
/*     */   
/*     */   public boolean isUsingAdminPrivileges() {
/* 637 */     return (this.mAuthToken != null) && (AuthToken.isAnyAdmin(this.mAuthToken));
/*     */   }
/*     */   
/*     */   public ZimbraSoapContext disableNotifications() {
/* 641 */     this.mSessionEnabled = false;
/* 642 */     this.mSessionInfo = null;
/* 643 */     return this;
/*     */   }
/*     */   
/*     */   public boolean isNotificationEnabled() {
/* 647 */     return this.mSessionEnabled;
/*     */   }
/*     */   
/*     */   public boolean isSessionProxied() {
/* 651 */     return this.mSessionProxied;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   SessionInfo getSessionInfo()
/*     */   {
/* 660 */     return this.mSessionInfo;
/*     */   }
/*     */   
/*     */   public SessionInfo setProxySession(String sessionId) {
/* 664 */     if ((!this.mSessionEnabled) || (sessionId == null)) {
/* 665 */       this.mSessionInfo = null;
/*     */     } else {
/* 667 */       this.mSessionInfo = new SessionInfo(sessionId, 0, false);
/*     */     }
/* 669 */     this.mSessionProxied = true;
/* 670 */     return this.mSessionInfo;
/*     */   }
/*     */   
/*     */   SessionInfo recordNewSession(String sessionId) {
/* 674 */     if ((!this.mSessionEnabled) || (sessionId == null)) {
/* 675 */       this.mSessionInfo = null;
/* 676 */     } else if (this.mSessionInfo == null) {
/* 677 */       this.mSessionInfo = new SessionInfo(sessionId, 0, true);
/*     */     } else {
/* 679 */       this.mSessionInfo.sessionId = sessionId;this.mSessionInfo.created = true;
/*     */     }
/* 681 */     return this.mSessionInfo;
/*     */   }
/*     */   
/*     */   protected void clearSessionInfo() {
/* 685 */     this.mSessionInfo = null;
/* 686 */     this.mSessionProxied = false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean hasCreatedSession()
/*     */   {
/* 697 */     return (this.mSessionInfo != null) && (this.mSessionInfo.created);
/*     */   }
/*     */   
/*     */   public boolean hasSession() {
/* 701 */     return this.mSessionInfo != null;
/*     */   }
/*     */   
/*     */   public boolean beginWaitForNotifications(Continuation continuation, boolean includeDelegates) throws ServiceException {
/* 705 */     this.mWaitForNotifications = true;
/* 706 */     this.continuationResume = new ResumeContinuationListener(continuation);
/*     */     
/* 708 */     Session session = SessionCache.lookup(this.mSessionInfo.sessionId, this.mAuthTokenAccountId);
/* 709 */     if (!(session instanceof SoapSession)) {
/* 710 */       return false;
/*     */     }
/* 712 */     SoapSession ss = (SoapSession)session;
/* 713 */     SoapSession.RegisterNotificationResult result = ss.registerNotificationConnection(this.mSessionInfo.getPushChannel(!includeDelegates));
/* 714 */     switch (result) {
/* 715 */     case NO_NOTIFY:  return false;
/* 716 */     case DATA_READY:  return false;
/* 717 */     case BLOCKING:  return true; }
/* 718 */     return false;
/*     */   }
/*     */   
/*     */   public void suspendAndUndispatch(long timeout)
/*     */   {
/* 723 */     this.continuationResume.suspendAndUndispatch(timeout);
/*     */   }
/*     */   
/*     */   public synchronized void signalNotification(boolean canceled)
/*     */   {
/* 728 */     this.mWaitForNotifications = false;
/* 729 */     this.mCanceledWaitForNotifications = canceled;
/* 730 */     this.continuationResume.resumeIfSuspended();
/*     */   }
/*     */   
/*     */   public synchronized boolean isCanceledWaitForNotifications() {
/* 734 */     return this.mCanceledWaitForNotifications;
/*     */   }
/*     */   
/*     */   public synchronized boolean waitingForNotifications() {
/* 738 */     return this.mWaitForNotifications;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   Element toProxyContext(SoapProtocol proto, boolean excludeAccountDetails)
/*     */   {
/* 748 */     Element ctxt = proto.getFactory().createElement(HeaderConstants.CONTEXT);
/*     */     
/* 750 */     ctxt.addAttribute("hops", this.mHopCount);
/*     */     
/* 752 */     String proxyAuthToken = null;
/*     */     
/* 754 */     if (this.mAuthToken != null) {
/* 755 */       proxyAuthToken = this.mAuthToken.getProxyAuthToken();
/*     */     }
/*     */     
/* 758 */     if (proxyAuthToken != null) {
/* 759 */       new ZAuthToken(proxyAuthToken).encodeSoapCtxt(ctxt);
/* 760 */     } else if (this.mRawAuthToken != null) {
/* 761 */       this.mRawAuthToken.encodeSoapCtxt(ctxt);
/*     */     }
/* 763 */     if (this.mResponseProtocol != this.mRequestProtocol) {
/* 764 */       ctxt.addNonUniqueElement("format").addAttribute("type", this.mResponseProtocol == SoapProtocol.SoapJS ? "js" : "xml");
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 769 */     if (!this.mSessionEnabled)
/*     */     {
/* 771 */       ctxt.addUniqueElement("nosession");
/* 772 */     } else if (this.mSessionInfo != null) {
/* 773 */       encodeSession(ctxt, this.mSessionInfo.sessionId, null).addAttribute("proxy", true);
/*     */     } else {
/* 775 */       ctxt.addUniqueElement("session").addAttribute("proxy", true);
/*     */     }
/*     */     
/* 778 */     if (!excludeAccountDetails) {
/* 779 */       Element eAcct = ctxt.addNonUniqueElement("account").addAttribute("link", this.mMountpointTraversed);
/*     */       
/* 781 */       if ((this.mRequestedAccountId != null) && (!this.mRequestedAccountId.equalsIgnoreCase(this.mAuthTokenAccountId))) {
/* 782 */         eAcct.addAttribute("by", "id").setText(this.mRequestedAccountId);
/*     */       }
/*     */     }
/*     */     
/* 786 */     if (this.mUnqualifiedItemIds) {
/* 787 */       ctxt.addUniqueElement("noqualify");
/*     */     }
/* 789 */     Element ua = ctxt.addUniqueElement("userAgent");
/* 790 */     ua.addAttribute("name", "ZCS");
/* 791 */     ua.addAttribute("version", BuildInfo.VERSION);
/* 792 */     ctxt.addUniqueElement("via").setText(getNextVia());
/* 793 */     if (null != this.soapRequestId) {
/* 794 */       ctxt.addUniqueElement("soapId").setText(this.soapRequestId);
/*     */     }
/* 796 */     return ctxt;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   Element toProxyContext(SoapProtocol proto)
/*     */   {
/* 806 */     return toProxyContext(proto, false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Element encodeSession(Element parent, String sessionId, Session.Type sessionType)
/*     */   {
/* 821 */     Element oldSession = parent.getOptionalElement("session");
/* 822 */     if (oldSession != null) {
/* 823 */       oldSession.detach();
/*     */     }
/* 825 */     String typeStr = sessionType == Session.Type.ADMIN ? "admin" : null;
/* 826 */     Element eSession = parent.addUniqueElement("session");
/* 827 */     eSession.addAttribute("type", typeStr).addAttribute("id", sessionId).setText(sessionId);
/* 828 */     return eSession;
/*     */   }
/*     */   
/*     */   public SoapProtocol getRequestProtocol() {
/* 832 */     return this.mRequestProtocol;
/*     */   }
/*     */   
/*     */   public SoapProtocol getResponseProtocol() {
/* 836 */     return this.mResponseProtocol;
/*     */   }
/*     */   
/*     */   public Element createElement(String name) {
/* 840 */     return this.mResponseProtocol.getFactory().createElement(name);
/*     */   }
/*     */   
/*     */   public Element createElement(QName qname) {
/* 844 */     return this.mResponseProtocol.getFactory().createElement(qname);
/*     */   }
/*     */   
/*     */   public Element createRequestElement(String name) {
/* 848 */     return this.mRequestProtocol.getFactory().createElement(name);
/*     */   }
/*     */   
/*     */   public Element createRequestElement(QName qname) {
/* 852 */     return this.mRequestProtocol.getFactory().createElement(qname);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public Element jaxbToElement(Object resp)
/*     */     throws ServiceException
/*     */   {
/* 860 */     return JaxbUtil.jaxbToElement(resp, this.mResponseProtocol.getFactory());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Element jaxbToNamedElement(String name, String namespace, Object o)
/*     */     throws ServiceException
/*     */   {
/* 870 */     return JaxbUtil.jaxbToNamedElement(name, namespace, o, this.mResponseProtocol.getFactory());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public AuthToken getAuthToken()
/*     */   {
/* 880 */     return this.mAuthToken;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZAuthToken getRawAuthToken()
/*     */   {
/* 890 */     return this.mRawAuthToken;
/*     */   }
/*     */   
/*     */   public ProxyTarget getProxyTarget() {
/* 894 */     return this.mProxyTarget;
/*     */   }
/*     */   
/*     */   public boolean isProxyRequest() {
/* 898 */     return this.mIsProxyRequest;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public String getUserAgent()
/*     */   {
/* 906 */     return this.mUserAgent;
/*     */   }
/*     */   
/*     */   public String getRequestIP() {
/* 910 */     return this.mRequestIP;
/*     */   }
/*     */   
/*     */   public String getSoapRequestId() {
/* 914 */     return this.soapRequestId;
/*     */   }
/*     */   
/*     */   public void setSoapRequestId(String soapId) {
/* 918 */     this.soapRequestId = soapId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   String getVia()
/*     */   {
/* 927 */     return this.mVia;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   String getNextVia()
/*     */   {
/* 936 */     StringBuilder result = new StringBuilder();
/*     */     
/* 938 */     if (this.mVia != null) {
/* 939 */       result.append(this.mVia);
/* 940 */       result.append(',');
/*     */     }
/*     */     
/* 943 */     result.append(this.mRequestIP != null ? this.mRequestIP : Character.valueOf('?'));
/* 944 */     result.append('(');
/* 945 */     result.append(this.mUserAgent != null ? this.mUserAgent : Character.valueOf('?'));
/* 946 */     result.append(')');
/*     */     
/* 948 */     return result.toString();
/*     */   }
/*     */   
/*     */   boolean getChangeConstraintType()
/*     */   {
/* 953 */     return this.mChangeConstraintType;
/*     */   }
/*     */   
/*     */   int getChangeConstraintLimit() {
/* 957 */     return this.mMaximumChangeId;
/*     */   }
/*     */   
/*     */   public boolean wantsUnqualifiedIds() {
/* 961 */     return this.mUnqualifiedItemIds;
/*     */   }
/*     */   
/*     */   private void setHopCount(int hopCount) throws ServiceException {
/* 965 */     if (hopCount > 5)
/* 966 */       throw ServiceException.TOO_MANY_HOPS(this.mRequestedAccountId);
/* 967 */     this.mHopCount = hopCount;
/*     */   }
/*     */   
/*     */   public int getHopCount() {
/* 971 */     return this.mHopCount;
/*     */   }
/*     */   
/*     */   public void resetProxyAuthToken() {
/* 975 */     this.mAuthToken.resetProxyAuthToken();
/* 976 */     this.mRawAuthToken.resetProxyAuthToken();
/*     */   }
/*     */   
/*     */   public boolean isAuthUserOnLocalhost() {
/* 980 */     if (this.mAuthTokenAccountId != null) {
/*     */       try {
/* 982 */         Account a = Provisioning.getInstance().getAccountById(this.mAuthTokenAccountId);
/* 983 */         if (a != null) {
/* 984 */           return Provisioning.onLocalServer(a);
/*     */         }
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     }
/* 989 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/soap/ZimbraSoapContext.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */